const { ccclass, property } = cc._decorator;

@ccclass
export default class BsurceMainUICtrl extends cc.Component {
    @property(cc.Node)
    bottomNode = null;

    @property(cc.Node)
    tabNode = null;

    @property(cc.Node)
    cityListNode = null;

    @property(cc.Node)
    content = null;

    @property(cc.Node)
    webNode = null;

    @property(cc.Node)
    aboutAs = null;

    @property(cc.Node)
    rankListScrollView = null;

    @property(cc.Node)
    rankView = null;

    @property(cc.Node)
    discoveryScrollView = null;

    @property(cc.Node)
    discoveryView = null;

    @property(cc.Prefab)
    rankItem = null;

    @property(cc.Node)
    findNode = null;

    onLoad() {
        this.addEvent();
        this.showTabs(null, '1');
    }

    addEvent() {
        cc.director.getEventDispatcher().addCustomEventListener('SHOW_CITY_LIST', this.dealCityList.bind(this));
        for (let i = 0; i < 16; i++) {
            let btn = cc.find(`btn_${i+1}`, this.discoveryView);
            if (btn) {
                btn.on(cc.Node.EventType.TOUCH_END, () => {
                    if (d.cityConfig) {
                        let city = d.cityConfig.mapArr[i];
                        if (this.findNode) {
                            this.findNode.active = true;
                            let findCtrl = this.findNode.getComponent('FindNodeViewCtrl');
                            if (findCtrl) {
                                findCtrl.initView(city);
                            }
                        }
                    }
                }, this);
            }
        }
    }

    showTabs(e, t) {
        let tabIndex = parseInt(t);
        if (this.tabIndex != tabIndex) {
            this.tabIndex = tabIndex;
            this.showOneSelect(t);
            this.showCurTabs();
            if (this.tabIndex === 2) {
                this.showHotRecommend();
            } else if (this.tabIndex === 3) {
                this.showRankList();
            } else if (this.tabIndex === 4) {
                this.scheduleOnce(() => {
                    this.discoveryView.height = this.discoveryScrollView.height;
                });
            }
        } else {
            console.log('重复点击了=======');
        }
    }

    showHotRecommend() {
        this.tabNode.children[this.tabIndex - 1].getComponent('HotRecommendCtrl').createCityList();
    }

    showRankList() {
        if (this.content.childrenCount === 0) {
            this.scheduleOnce(() => {
                this.rankView.height = this.rankListScrollView.height;
            });
            for (let i = 0; i < 10; i++) {
                let item = cc.instantiate(this.rankItem);
                item.getComponent('RankItemCtrl').initData({ index: i });
                this.content.addChild(item);
            }
        }
    }

    showCurTabs() {
        let tabChildren = this.tabNode.children;
        for (let i = 0; i < tabChildren.length; i++) {
            tabChildren[i].active = this.tabIndex - 1 === i;
        }
        this.webNode.active = false;
        this.aboutAs.active = false;
    }

    showOneSelect(e) {
        let bottomChildren = this.bottomNode.children;
        for (let i = 0; i < bottomChildren.length; i++) {
            let btn = bottomChildren[i];
            let clickEvent = btn.getComponent(cc.Button).clickEvents[0];
            let customEventData = clickEvent.customEventData;
            let r = btn.getChildByName("common");
            let n = btn.getChildByName("select");
            let l = btn.getChildByName("des");
            if (customEventData === e) {
                r.active = false;
                n.active = true;
                l.color = new cc.Color().fromHEX("#000000");
            } else {
                r.active = true;
                n.active = false;
                l.color = new cc.Color().fromHEX("#979797");
            }
        }
    }

    dealCityList(e) {
        if (e) {
            this.cityListNode.active = true;
            this.cityListNode.getComponent('ImageTouchCtrl').initData(e);
        }
    }
}
